//
//  ZBBaseWKWebViewController.m
//  ZBMainFramework
//
//  Created by apple on 2018/10/24.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZB_BaseWKWebViewController.h"
#define TBBaseWKWebViewVC_KVO_EstimatedProgress @"estimatedProgress"

@interface ZB_BaseWKWebViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic, strong) NSString *url;
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation ZB_BaseWKWebViewController

- (instancetype)initWithUrl:(NSString *)url{
    if (self = [super init]) {
        self.url = url;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    WS(weakSelf);
    [self setBaseNavigationBarTitle:self.title];
    
    [self.baseContentView addSubview:self.webView];
    [self.naviBarContentView addSubview:self.progressView];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.right.left.top.bottom.equalTo(weakSelf.baseContentView);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(weakSelf.naviBarContentView);
        make.height.mas_equalTo(2);
    }];
    
    [weakSelf loadRequest];
    
    [self.webView addObserver:self forKeyPath:TBBaseWKWebViewVC_KVO_EstimatedProgress options:NSKeyValueObservingOptionNew context:nil];
    
}
- (void)loadRequest
{
    NSString *tempURL = [self.url stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString  *UTF8URL =  [tempURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:UTF8URL]];
    [self.webView loadRequest:request];
    
    
}

#pragma mark -- WKWebView

#pragma mark- KVO
//进度值的监听
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
    //网页加载完毕隐藏进度条
    self.progressView.hidden = (self.webView.estimatedProgress >= 1.0);
    
}

#pragma mark- WKNavigationDelegate
//webView的代理方法
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    decisionHandler(WKNavigationActionPolicyAllow);
}

#pragma mark - lanjz

- (WKWebView *)webView
{
    if(_webView == nil)
    {
        // 根据生成的WKUserScript对象，初始化WKWebViewConfiguration
        NSMutableString *javascript = [NSMutableString string];
        [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按
        WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        
        //设置configur对象的preferences属性的信息
        WKPreferences *preference = [[WKPreferences alloc]init];
        //是否允许与js进行交互，默认是YES的，如果设置为NO，js的代码就不起作用了
        preference.javaScriptEnabled = NO;
        config.preferences = preference;
        
        //增加观察者进度
        [_webView addObserver:self forKeyPath:TBBaseWKWebViewVC_KVO_EstimatedProgress options:NSKeyValueObservingOptionNew context:nil];
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
        _webView.navigationDelegate = self;
        _webView.opaque = NO;
        _webView.scrollView.scrollEnabled = YES;
        _webView.UIDelegate = self;
        [_webView sizeToFit];
        _webView.backgroundColor = [UIColor whiteColor];
        
        [_webView.configuration.userContentController addUserScript:noneSelectScript];
    }
    return _webView;
}
- (UIProgressView *)progressView
{
    if(_progressView == nil)
    {
        _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.progressTintColor = [UIConstantColor getThemeColor];
        _progressView.trackTintColor = [UIColor whiteColor];
    }
    return _progressView;
}
-(void)dealloc{
    NSLog(@"%@已经销毁",self);
    [self.webView removeObserver:self forKeyPath:TBBaseWKWebViewVC_KVO_EstimatedProgress];
}

@end
